#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>

std::atomic<int> data{0};

void producer() {
    std::this_thread::sleep_for(std::chrono::seconds(1)); // 模拟数据生产延时
    data.store(1, std::memory_order_release); // 设置 data 为 1
    data.notify_one(); // 通知等待的线程
    std::cout << "Producer set data to 1 and notified." << std::endl;
}

void consumer() {
    std::cout << "Consumer is waiting for data..." << std::endl;
    data.wait(0, std::memory_order_acquire); // 等待 data 变为非0
    std::cout << "Consumer received data: " << data.load(std::memory_order_acquire) << std::endl;
}

int main() {
    std::thread consumerThread(consumer);
    std::thread producerThread(producer);

    producerThread.join();
    consumerThread.join();

    return 0;
}